﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools;
using System.Windows.Forms;
using MARS.analyticalengine;

namespace Mars2013plugin
{
    public partial class ThisAddIn
    {
        private Dictionary<Outlook.Inspector, InspectorWrapper> inspectorWrappersValue = new Dictionary<Outlook.Inspector, InspectorWrapper>();
        public Dictionary<Outlook.MailItem, MarsAnalyticsRegion> MarsRegionsDict = new Dictionary<Outlook.MailItem, MarsAnalyticsRegion>();
        private Outlook.Inspectors inspectors;
        public MARSanalyticsEngine MARSEngine = null;
        public Boolean bCheckHyperLinks = false;
        public Boolean bCheckTrailLines = false;
        public Boolean bCheckDateTime = false;
        public int nLinesToSkip = 4;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            inspectors = this.Application.Inspectors;

            try
            {
                
                MARSEngine = new MARSanalyticsEngine();
            }
            catch(Exception ex)
            {
                MessageBox.Show("MARS Analytic Engine could not be Initialized.\n Error:" + ex.Message + "\n\n" + ex.TargetSite,"FATAL ERROR");
                MARSEngine = null;
            }
            //Add event Handlers
           // inspectors.NewInspector +=
            //new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);

            //this.Application.NewMail += new Outlook.ApplicationEvents_11_NewMailEventHandler(Application_NewMail);
            //this.Application.NewMailEx += new Outlook.ApplicationEvents_11_AttachmentContextMenuDisplayNewMailExEventHandler(Application_NewMailEx);
            //this.Application.ItemLoad += new Outlook.ApplicationEvents_11_ItemLoadEventHandler(Application_ItemLoad);
            this.Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
            //this.Application. += new Outlook.ApplicationEvents_11_AttachmentContextMenuDisplayEventHandler(Application_AttachmentContextMenuDisplay);
            //this.Application.ContextMenuClose += new Outlook.ApplicationEvents_11_ContextMenuCloseEventHandler(Application_ContextMenuClose);
            //this.Application.FolderContextMenuDisplay += new Outlook.ApplicationEvents_11_FolderContextMenuDisplayEventHandler(Application_FolderContextMenuDisplay);
            //this.Application.ItemContextMenuDisplay += new Outlook.ApplicationEvents_11_ItemContextMenuDisplayEventHandler(Application_ItemContextMenuDisplay);
            //this.Application.OptionsPagesAdd += new Outlook.ApplicationEvents_11_OptionsPagesAddEventHandler(Application_OptionsPagesAdd);
            //this.Application.Startup += new Outlook.ApplicationEvents_11_StartupEventHandler(Application_Startup);
            //this.Application.StoreContextMenuDisplay += new Outlook.ApplicationEvents_11_StoreContextMenuDisplayEventHandler(Application_StoreContextMenuDisplay);
            //this.Application.Explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer);
            
            foreach(Outlook.Inspector inspector in inspectors)
            {
                Inspectors_NewInspector(inspector);
            }
        }

        void Explorers_NewExplorer(Outlook.Explorer Explorer)
        {
            //WindowFormRegionCollection formRegions =
            //    Globals.FormRegions
            //        [Globals.ThisAddIn.Application.ActiveInspector()];
            //formRegions.MarsFormRegion.textBox1.Text = "Active Inspector - Explorers_NewExplorer";

            //formRegions =
            //    Globals.FormRegions
            //        [Globals.ThisAddIn.Application.ActiveExplorer()];
            //    formRegions.MarsFormRegion.textBox1.Text = "Active Explorer Explorers_NewExplorer";
        }

        void Application_StoreContextMenuDisplay(Office.CommandBar CommandBar, Outlook.Store Store)
        {
           
        }

        void Application_Startup()
        {
           
        }

        void Application_OptionsPagesAdd(Outlook.PropertyPages Pages)
        {
           
        }

        void Application_ItemContextMenuDisplay(Office.CommandBar CommandBar, Outlook.Selection Selection)
        {
           
        }

        void Application_FolderContextMenuDisplay(Office.CommandBar CommandBar, Outlook.MAPIFolder Folder)
        {
           
        }
         
        void Application_ContextMenuClose(Outlook.OlContextMenu ContextMenu)
        {
           
        }

        void Application_AttachmentContextMenuDisplay(Office.CommandBar CommandBar, Outlook.AttachmentSelection Attachments)
        {
           
        }

        void Application_ItemSend(object Item, ref bool Cancel)
        {
            //MessageBox.Show("Hit Send");

            Outlook.Inspector curInspector = Globals.ThisAddIn.Application.ActiveInspector();
            if (!Globals.ThisAddIn.MarsRegionsDict.ContainsKey((Outlook.MailItem)Item))
            {
                MessageBox.Show("Region Not found!");
            }
            if (curInspector != null)
            {
                WindowFormRegionCollection formRegions = Globals.FormRegions[curInspector];
                if (formRegions != null)
                {
                    if (formRegions.MarsFormRegion.pr == null)
                    {
                        DialogResult result = MessageBox.Show("This email has not been analysed would you like to analyze it before send ?", "MARS Analytics", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            formRegions.MarsFormRegion.RunAnalysis();
                        }
                        else
                            return;

                    }

                    string tonestr = (formRegions.MarsFormRegion.pr.EmailTone == 1) ? "Mildly rude" : "Obscene";

                    if (formRegions.MarsFormRegion.pr.EmailTone > 0)
                    {
                        DialogResult result = MessageBox.Show("The Tone of this email appears to be " + tonestr + " Are you sure you want to send this ?", "MARS Analytics", MessageBoxButtons.OKCancel);
                        if (result == DialogResult.Cancel)
                        {
                            Cancel = true;
                            return;
                        }
                    }
                }
            }
        }

        void Application_ItemLoad(object Item)
        {
           
        }

        void Application_NewMailEx(string EntryIDCollection)
        {
           
        }

        void Application_NewMail()
        {
           
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            inspectors.NewInspector -=
            new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
            inspectors = null;
            inspectorWrappersValue = null;
        }

        void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
        {
            WindowFormRegionCollection formRegions;

            if (Globals.ThisAddIn.Application.ActiveInspector() != null)
            { 
                formRegions =
                    Globals.FormRegions
                        [Globals.ThisAddIn.Application.ActiveInspector()];
                formRegions.MarsFormRegion.richTextBox = "Active Inspector - Inspectors_NewInspector";
            }
            if (Globals.ThisAddIn.Application.ActiveExplorer() != null)
            {
                

                formRegions =
                    Globals.FormRegions
                        [Globals.ThisAddIn.Application.ActiveExplorer()];
                formRegions.MarsFormRegion.richTextBox = "Active Explorer Inspectors_NewInspector";
            }
            if (Inspector.CurrentItem is Outlook.MailItem)
            {
                inspectorWrappersValue.Add(Inspector, new InspectorWrapper(Inspector));

                //if (mailItem.EntryID == null)
                //{
                //    mailItem.Subject = "This text was added by using code";
                //    mailItem.Body = "This text was added by using code";
                //}

            }
        }

        public Dictionary<Outlook.Inspector, InspectorWrapper> InspectorWrappers
        {
            get
            {
                return inspectorWrappersValue;
            }
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }


}
